/**
 * 
 */
package com.local.callable.CallableDemo;

import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author Administrator
 *
 */

class MyCallable implements Callable<String> {
    private String name;
    private static int frequency = 0;

    public static int getFrequency() {
    	return frequency;
    }
    public static void setFrequency(int frequency) {
    	MyCallable.frequency = frequency;
    }
	public MyCallable(String name){
		this.name = name;
	}
	@Override
	public String call() throws Exception {
		for(int i=0; i<10; i++) {
			frequency = frequency + 1;
		}
		return name+"执行中..."+frequency;
	}  
	
}

public class CallableDemo {

	/**
	 * @param args
	 * @throws ExecutionException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		// TODO Auto-generated method stub
		 ExecutorService exec = Executors.newCachedThreadPool();  
	     ArrayList<Future<String>> results = new ArrayList<Future<String>>();    
	     //Future 相当于是用来存放Executor执行的结果的一种容器  
//	     for (int i = 0; i < 10; i++) {  
//	    	 results.add(exec.submit(new Callable<String>(){
//				@Override
//				public String call() throws Exception {	
//					return Thread.currentThread().getName()+"call。。。";
//				}
//	    		 
//	    	 }));  
//	     }
	     
	     for(int i=0; i<10; i++) {
	    	 results.add(exec.submit(new MyCallable("线程"+i)));
	     }
	     
	     for(int i=0; i<results.size(); i++) {
	    	 Future<String> fs = results.get(i);
	    	 if (fs.isDone()) {  
	                System.out.println(fs.get());  
	            } else {  
	                System.out.println("执行未完成");  
	            }  
	     }     
	     exec.shutdown();
	}

}
